﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace GCard
{
    public partial class LoginForm : Form
    {
        private string connStr = ("Data Source= sqliteLogin.db;Version=3;New=True;Compress=True;");
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            //后面拼写查询语句要用到窗体的信息
            string user = txt_user.Text;
            string pwd = txt_password.Text;
            //创建数据库连接类的对象
            //将连接打开
            //执行con对象的函数，返回一个SqlCommand类型的对象
            //把输入的数据拼接成sql语句，并交给cmd对象
            //用cmd的函数执行语句，返回SqlDataReader对象dr,dr就是返回的结果集（也就是数据库中查询到的表数据）
            //用dr的read函数，每执行一次，返回一个包含下一行数据的集合dr，在执行read函数之前，dr并不是集合
            //用完后关闭连接，以免影响其他程序访问

            SQLiteConnection sqlite_conn = new SQLiteConnection(connStr);
            sqlite_conn.Open();
            SQLiteDataReader sqlite_datareader;
            SQLiteCommand sqlite_cmd;
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "select* from Table_Login where user='" + user + "'and pwd='" + pwd + "'";

            sqlite_datareader = sqlite_cmd.ExecuteReader();
            while (sqlite_datareader.Read())
            {
                string myreader = sqlite_datareader.GetString(0);
                MessageBox.Show("登录成功");
            }
            sqlite_conn.Close();
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
            string user = txt_user.Text;
            string pwd = txt_password.Text;
            //创建数据库连接类的对象
            //执行con对象的函数，返回一个SqlCommand类型的对象
            //拼写语句
            //增删改用ExecuteNonQuery，会返回一个整型数字

            SQLiteConnection sqlite_conn = new SQLiteConnection(connStr);
            sqlite_conn.Open();

            SQLiteCommand sqlite_cmd;
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "insert into Table_Login values('" + user + "','" + pwd + "')";
            int cnt = sqlite_cmd.ExecuteNonQuery();
            if (cnt > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            sqlite_conn.Close();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            string user = txt_user.Text;
            string pwd = txt_password.Text;
            //创建数据库连接类的对象
            //执行con对象的函数，返回一个SqlCommand类型的对象
            //拼写语句
            //增删改用ExecuteNonQuery，会返回一个整型数字
            SQLiteConnection sqlite_conn = new SQLiteConnection(connStr);
            sqlite_conn.Open();

            SQLiteCommand sqlite_cmd;
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "delete from Table_Login where user='" + user + "' ";
            int count = sqlite_cmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            sqlite_conn.Close();
        }
    }
}
